/*
 * PLauncher - Live2D Virtual Desktop Assistant
 * https://github.com/Pfolg/PLauncher
 * Copyright (c) 2025 SY Cheng
 *
 * MIT License
 * https://github.com/Pfolg/PLauncher/blob/main/LICENSE
 */
#pragma once

#include <QProcess>
#include <QDesktopServices>
#include <QUrl>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QPair>
#include <QFuture>
#include <QtConcurrent>
#include "tray.h"


static QFuture<void> launchByPathAsync(const QString &path) {
    qDebug()<<"launching: "<<path;
    return QtConcurrent::run([path]() -> bool {
        try {
            bool success = false;

            // 对于URL，使用QDesktopServices
            if (path.startsWith("http://") || path.startsWith("https://") ||
                path.startsWith("ftp://") || path.startsWith("file://")) {
                success = QDesktopServices::openUrl(QUrl(path));
            }
            // 对于本地文件/文件夹，使用QDesktopServices
            else {
                QFileInfo fileInfo(path);
                if (fileInfo.exists()) {
                    QUrl localUrl = QUrl::fromLocalFile(QDir::toNativeSeparators(path));
                    success = QDesktopServices::openUrl(localUrl);
                } else {
                    qWarning() << "File or directory does not exist:" << path;
                    TrayIcon::instance()->showMessage("Warning", "File or directory does not exist:" + path, QSystemTrayIcon::Warning, 5000);
                    return false;
                }
            }

            if (!success) {
                qWarning() << "Failed to open:" << path;
                TrayIcon::instance()->showMessage("Warning", "Failed to open:" + path, QSystemTrayIcon::Warning, 5000);
                return false;
            }

            return true;

        } catch (const std::exception& e) {
            qCritical() << "Exception occurred while launching" << path << ":" << e.what();
            TrayIcon::instance()->showMessage("Error", "Exception occurred while launching:" + path + ":" + e.what(), QSystemTrayIcon::Critical, 5000);
            return false;
        } catch (...) {
            qCritical() << "Unknown exception occurred while launching" << path;
            TrayIcon::instance()->showMessage("Error", "Unknown exception occurred while launching:" + path, QSystemTrayIcon::Critical, 5000);
            return false;
        }
    });
}

// 同步版本
static void launchByPath(const QString &path) {
    launchByPathAsync(path);
}
